package net.sunofbeach.blog.controller.portal;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sunofbeach.blog.pojo.Article;
import net.sunofbeach.blog.pojo.Comment;
import net.sunofbeach.blog.pojo.User;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.response.ResultEnum;
import net.sunofbeach.blog.service.ArticleService;
import net.sunofbeach.blog.service.CommentService;
import net.sunofbeach.blog.service.UserService;
import net.sunofbeach.blog.service.impl.TaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Api(tags = "门户评论管理")
@RestController
@RequestMapping("/portal/comment")
public class CommentPortalApi {

    @Resource
    private UserService userService;

    @Resource
    private ArticleService articleService;

    @Resource
    private CommentService commentService;

    @Resource
    private TaskService taskService;

    /**
     * 添加评论
     *
     * @return
     */
    @ApiOperation("新增评论")
    @PostMapping
    public Result addComment(@RequestBody Comment comment) {
        // 查检用户是否有登录
        User user = userService.checkSobUser();
        if (user == null) {
            // 账号未登录
            return Result.build(null, ResultEnum.ACCOUNT_NOT_LOGIN);
        }
        // 检查内容
        String articleId = comment.getArticleId();
        if (StringUtils.isEmpty(articleId)) {
            return Result.fail().message("文章ID不能为空");
        }
        Article article = articleService.getById(articleId);

        if (article == null) {
            return Result.fail().message("文章不存在");
        }
        if (StringUtils.isEmpty(comment.getContent())) {
            return Result.fail().message("评论内容不能为空");
        }
        // 补全数据
        comment.setUserAvatar(user.getAvatar());
        comment.setUsername(user.getUsername());
        comment.setUserId(user.getId());
        // 保存入库
        commentService.save(comment);
        // TODO 发送邮件通知
        // 调用异步方法发送邮件
        User userFromDb = userService.getById(article.getUserId());
        if (userFromDb.getEmail() != null && comment.getParentContent() == null) {
            try {
                taskService.sendCommentNotifyEmail(userFromDb.getEmail(), article.getTitle(), comment.getContent());
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(e).message("新增评论发送邮件通知文章作者失败");
            }
        }
        // 返回结果
        return Result.ok().message("发表评论成功");
    }


    /**
     * 列表
     * 根据文章ID查询评论列表
     *
     * @return
     */
    @ApiOperation("根据文章ID查询评论列表")
    @GetMapping("/list/{articleId}/{page}/{size}")
    public Result listComments(@PathVariable("articleId") String articleId,
                               @PathVariable("page") int page, @PathVariable("size") int size) {
        Page<Comment> pageParam = new Page<>(page, size);
        IPage<Comment> pageModel = commentService.selectPage(pageParam, articleId);
        return Result.ok(pageModel);
    }



    /**
     * 删除
     *
     * @param commentId
     * @return
     */
    @ApiOperation("根据评论ID删除评论")
    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable("commentId") String commentId) {

        return commentService.removeByCommentId(commentId);
    }


}

